/*
 * $RCSfile: Processor_46_Msg.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main.processor;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.TsUtil;
import com.i366.room.model.HuoDong;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.db.UserInfoDao;
import com.work.db.imp.UserInfoDaoImp;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 * 
 * ����ʱ����
 * <p>Title: Processor_62_Persion</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_64_Honus extends Handler{

	private Logger logger = Logger.getLogger(Processor_64_Honus.class);
	public byte[] handleRequest() {
		logger.debug("Processor_64_Honus userId is : " + this.getGameRequest().getUserId());
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int userid = request.getUserId();
		try {
			UserInfo userInfo = Cache.getOnlineUserInfo().get(userid);
			if (userInfo == null) {
				return pusUser(1); 
			}
			Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
			if (room == null) {
				return pusUser(1); 
			}
			RoomPersion p = room.getAudMap().get(userid);
			if (p == null) {
				return pusUser(1); 
			}

			//
			//��¼����ʱ
			Long l;
			if ( p.getAddUpTime() < 100) {
				l = new Long(p.getAddUpTimeLeiji()/1000);
			}else {
				l =	(System.currentTimeMillis() - p.getAddUpTime() + p.getAddUpTimeLeiji())/1000;
				logger.debug("System.currentTimeMillis() - p.getAddUpTime()=" + (System.currentTimeMillis() - p.getAddUpTime()));
			}
			l = l + (p.getUserInfo().getHuoDong().getAddUpTime()) * 60;
			int ll = l.intValue();
			int lll = -1; //���λ����ʱ
			int honu = -1;//��Ӧ���뽱��
			int chip2 = 0;//���һ�ε���ʱ����
			//��һ���
			int[] syc = null;//shangһ����ʱ����
			int k = 0;
			logger.debug("userId:" + userid + " �Ѿ�����ʱ��" + ll);
			for (int[] ints : HuoDong.HUO_DONG1) {
				if (ll < ints[1] * 60) {
					lll = ints[1]*60 - ll;
					honu = HuoDong.HUO_DONG1_CHIP[k];
					if (k > 0) {
						syc = HuoDong.HUO_DONG1[k -1];
						chip2 = HuoDong.HUO_DONG1_CHIP[k - 1];
					}
					break;
				}
				k ++;
			}
			if (syc == null && ll > ( HuoDong.HUO_DONG1[HuoDong.HUO_DONG1.length -1][1] * 60 - 10)) {
				syc = HuoDong.HUO_DONG1[HuoDong.HUO_DONG1.length -1];
				chip2 = HuoDong.HUO_DONG1_CHIP[HuoDong.HUO_DONG1_CHIP.length -1];
			}
			UserInfoDao dao = new UserInfoDaoImp();
			if (syc != null) {
				int count = dao.getTaskLogSize(syc[0], userInfo.getUserId());
				if (count != 0) {
					syc = null;
				}
			}
			logger.debug("daojishi chip : chip:" + chip2 + " lll:" + lll + " honuChip:" + honu);
			room.getRoomService().daojishi(p, chip2, syc, lll, honu);
			//���log
			if (syc != null) {
				dao.inserUserLiwuChips(p.getUserId(), syc[0], 2, chip2);
			}
			//
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
			}
			if (chip2 > -1) {
				String str = "������" + chip2 + "����!";
				byte[] bytes = TsUtil.getSendByte(str);
				return bytes;
			}
			return null;
		} catch (Exception e) {
			logger.debug("������Ϣ����", e);
			return pusUser(1);
		}
	}

	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GET_COUNTDOWN_HONUS);
		return bytes;
	}
	
	public Object makeObject() throws Exception {
		return new Processor_64_Honus();
	}
	
}

